package com.c.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.c.entity.Member;
import com.c.entity.Permission;
import com.c.entity.Role;
import com.c.entity.User;
import com.c.mapper.MemberMapper;
import com.c.mapper.PermissionMapper;
import com.c.mapper.RoleMapper;
import com.c.mapper.UserMapper;
import com.c.service.MemberService;
import com.c.service.UserService;
import com.c.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author wangxiangli
 * @create 2022-12-25-12:30
 */
@Service
@Transactional
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    /**
     * 根据页码查找会员
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        IPage<Member> pageMember=new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<Member> wrapper=new QueryWrapper();
        String queryString = queryPageBean.getQueryString();
        //判断条件值是否为空，如果不为空拼接条件
        if (queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0) {
            queryString = queryPageBean.getQueryString().trim();// 去除查询条件首尾空格
            wrapper.like("name",queryString);
            wrapper.or().like("phoneNumber",queryString);
            wrapper.or().like("fileNumber",queryString);
        }
        IPage page1 = baseMapper.selectPage(pageMember, wrapper);
        return new PageResult(page1.getTotal(),page1.getRecords());
    }

    /**
     * 保存会员信息
     *
     * @param member
     */
    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if (password != null) {
            //使用md5将明文密码进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        } else {
            //设置默认密码123456
            member.setPassword(MD5Utils.md5("123456"));
        }
        //设置注册时间
        member.setRegTime(new Date());
        baseMapper.insert(member);
    }

    /**
     * 手机号唯一性验证
     *
     * @param member
     * @return
     */
    @Override
    public String phoneValidator(Member member) {
        //获取表单提交的手机号
        String phoneNumber = member.getPhoneNumber();
        //获取表单提交的id
        Integer id = member.getId();
        //查询当前手机号是否已经被注册
        Integer phone = baseMapper.findByPhone(phoneNumber);
        //通过表单的id查询旗下手机号与表单提交的手机号进行对比
        Member byId = baseMapper.selectById(id);
        if (phone > 0 && id == null) {
            return "手机号已经存在";
        } else if (id != null && !byId.getPhoneNumber().equals(phoneNumber) && phone != 0) {
            return "手机号已经存在";
        } else {
            return "恭喜您通过验证";
        }
    }

    /**
     * 档案号唯一性验证
     *
     * @param member
     * @return
     */
    @Override
    public String fileNumberValidator(Member member) {
        //获取表单提交的档案号
        String fileNumber = member.getFileNumber();
        //获取表单提交的id
        Integer id = member.getId();
        //查询当前档案号已经被使用
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("fileNumber",fileNumber);
        Long byFileNumber = baseMapper.selectCount(wrapper);
        //通过表单的id查询旗下档案号与表单提交的档案号进行对比
        Member byId = baseMapper.selectById(id);
        if (byFileNumber > 0 && id == null) {
            return "档案号已经存在";
        } else if (id != null && !byId.getFileNumber().equals(fileNumber) && byFileNumber != 0) {
            return "档案号已经存在";
        } else {
            return "恭喜您通过验证";
        }
    }

    /**
     * 身份证唯一性验证
     *
     * @param member
     * @return
     */
    @Override
    public String idCardValidator(Member member) {
        //获取表单提交的身份证号
        String idCard = member.getIdCard();
        //获取表单提交的id
        Integer id = member.getId();
        //查询当前身份证号是否已经被注册
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("idCard",idCard);
        Long byIdCard = baseMapper.selectCount(wrapper);
        //通过表单的id查询旗下身份证号与表单提交的身份证号进行对比
        Member byId = baseMapper.selectById(id);
        if (byIdCard > 0 && id == null) {
            return "身份证已经存在";
        } else if (id != null && !byId.getIdCard().equals(idCard) && byIdCard != 0) {
            return "身份证已经存在";
        } else {
            return "恭喜您通过验证";
        }
    }
    /**
     * 邮箱唯一性验证
     *
     * @param member
     * @return
     */
    @Override
    public String emailValidator(Member member) {
        //获取表单提交的邮箱号
        String email = member.getEmail();
        //获取表单提交的id
        Integer id = member.getId();
        //查询当前邮箱号是否已经被注册
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("email",email);
        Long byEmailcount = baseMapper.selectCount(wrapper);
        //通过表单的id查询旗下邮箱号与表单提交的邮箱号进行对比
        Member byId =baseMapper.selectById(id);
        if (byEmailcount > 0 && id == null) {
            return "邮箱已经存在";
        } else if (id != null && byId.getEmail() != null && !byId.getEmail().equals(email) && byEmailcount != 0) {
            return "邮箱已经存在";
        } else {
            return "恭喜您通过验证";
        }
    }

    /**
     * 编辑会员信息
     *
     * @param member
     */
    @Override
    public void edit(Member member) {
        baseMapper.updateById(member);
    }
    /**
     * 删除会员信息
     *
     * @param id
     */
    @Override
    public Result deleteById(Integer id) {
        //判断当前订单表是否有关于该用户的订单（t_order）
        long count = baseMapper.findCountByCheckMemberId(id);
        //存在有关该会员的订单则无法删除
        if (count > 0) {
            return new Result(false, "存在有关该会员的订单,无法删除");
        } else {
            //删除会员信息（t_member）
            baseMapper.deleteById(id);
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        }
    }

    /**
     * 根据月份查询会员数量
     *
     * @param months
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        if (months != null && months.size() > 0) {
            for (String month : months) {
//                String date = month + "-30";//2018-05-31
                System.out.println("****************************"+month);
                Integer count = baseMapper.findMemberCountBeforeDate(month);
                memberCount.add(count);
            }
        }
        return memberCount;
    }


}
